
//
//  StockInquriyVC.m
//  ProjectforHDX
//
//  Created by Jaesun on 16/8/3.
//  Copyright © 2016年 H.DX. All rights reserved.
//

#import "StockInquiryVC.h"

#import "SearchBar.h" // 搜索框
#import "StockInquiryHeadView.h" // 自定义headView
#import "ShipmentReportHeadView.h" // 自定义headView
#import "StockInquiryCell.h" // 自定义cell
#import "ShipmentReportCell.h" // 自定义cell

#import "AFNetworking.h"

#import "PlateModel.h"

#import "StockInquryDetailVC.h"

#import "StockPopVIewController.h"

#import "MainVC.h"


typedef enum
{
    searchTypeFixpart=1,
    searchTypeFaultpart=2,
    searchTypeAll=3
}searchType;

@interface StockInquiryVC ()<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate,StockPopViewDelegate,UITextFieldDelegate> {
    /**
     *  cell数据 查询到的板件
     */
    NSMutableArray <PlateModel *>*dataArr;
    StockPopVIewController *_popView;
    BOOL hiden;
    UIButton *_typeBtn;
    UIButton *_storeNameBtn;
    //库存获取信息的数组
    NSMutableArray *stockArr;
    
    NSInteger teger;
    NSInteger tag;
    
    UIDatePicker *datePicker;
    UIToolbar *toolBar;
    
    UIButton *startBut;
    UIButton *endBut;
}

@property (nonatomic, strong) UISearchBar *searchBar;
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) UIView *coverView;

@property (nonatomic, strong) StockInquiryHeadView *headView;
@property (nonatomic, strong) ShipmentReportHeadView *SRHeadView;

//@property (nonatomic, assign) NSInteger *type;

@property (nonatomic, strong) UIView *searchHoldView;
/**
 *  网络请求参数
 */
@property (nonatomic, strong) NSMutableDictionary *postDict;
@property (nonatomic, strong) NSMutableDictionary *stockStrDict;
@property (nonatomic, strong) UIView *maskView;
@end

@implementation StockInquiryVC
{
    BOOL showPop;
}

static int type=1;

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
    stockArr = [NSMutableArray array];
    type = 1;
    self.postDict[@"type"] = [NSString stringWithFormat:@"%d",type];
    self.postDict[@"searchKey"]=@"";
    [self stockInquiry];
    if ([_stockStr isEqualToString:@"1"]) {
        [self shipmentReport];
    }else {
        [self InquiryInventory];
    }
    [_stockStrDict setValue:[THMangerTool getKey] forKey:@"Key"];
    [self setupDatas];
    [self setupViews];
}
#pragma mark 数据初始化

- (NSMutableDictionary *)postDict
{
    if (!_postDict) {
        _postDict = [NSMutableDictionary dictionary];
    }
    return _postDict;
}
- (NSMutableDictionary *)stockStrDict
{
    if (!_stockStrDict) {
        _stockStrDict = [NSMutableDictionary dictionary];
    }
    return _stockStrDict;
}

- (void)setupDatas {
    dataArr = [NSMutableArray array];
}



#pragma mark 视图初始化
- (void)setupViews {
    UIView *statusBarView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.height, 20)];
    
    statusBarView.backgroundColor=[UtilCheck hexStringToColor:@"E0E1E2" alpha:1.0];
    
    [self.view addSubview:statusBarView];
    
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
    
    {
        UIView *backGroundView = [[UIView alloc]init];
        backGroundView.backgroundColor = [UtilCheck hexStringToColor:@"E0E1E2" alpha:1.0];;
        [self.view addSubview:backGroundView];
        
        
        self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(50+30+50+120, 20, [UIScreen mainScreen].bounds.size.height-30-50-50-120, 44)];
        self.searchBar.delegate = self;
        self.searchBar.backgroundColor = [UIColor blackColor];
        self.searchBar.placeholder = [UtilCheck language:@"setting.inputPlate"];
        [self.view addSubview:self.searchBar];
        
        UIImage* searchBarBg = [self GetImageWithColor:[UtilCheck hexStringToColor:@"E0E1E2" alpha:1.0] andHeight:32.0f];
        //设置背景图片`
        [_searchBar setBackgroundImage:searchBarBg];
        
        
        UIButton *backBtn = [[UIButton alloc]init];
        [backBtn setFrame:CGRectMake(0, 27, 30, 30)];
        [backBtn addTarget:self action:@selector(backView) forControlEvents:UIControlEventTouchUpInside];
        backBtn.backgroundColor = [UtilCheck hexStringToColor:@"E0E1E2" alpha:1.0];
        [backBtn setImage:BDFILEIMAGENAEM(@"default/back_gray") forState:UIControlStateNormal];
        [self.view addSubview:backBtn];
        [backGroundView setFrame:CGRectMake(0, 20, [UIScreen mainScreen].bounds.size.height, 44)];
        
        UIButton *changeType = [[UIButton alloc]init];
        _typeBtn=changeType;
        changeType.titleLabel.font = CustomFont(13);
        [changeType setTitle:[UtilCheck language:@"order.search.fix"] forState:UIControlStateNormal];
        [self.view addSubview:changeType];
        [changeType setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        //        changeType.backgroundColor = [UtilCheck hexStringToColor:@"E0E1E2" alpha:1.0];
        [changeType addTarget:self action:@selector(changeSearchType:) forControlEvents:UIControlEventTouchUpInside];
        [changeType setFrame:CGRectMake(CGRectGetMaxX(backBtn.frame)+15, 28, 110, 28)];
        changeType.backgroundColor = [UIColor whiteColor];
        changeType.layer.cornerRadius = 5;
        [changeType setImage:BDFILEIMAGENAEM(@"Home/btn_dropGray") forState:UIControlStateNormal];
        changeType.imageEdgeInsets = UIEdgeInsetsMake(10, 105, 10, 0);
        
        UIButton *changeType1 = [[UIButton alloc]init];
        _storeNameBtn=changeType1;
        changeType1.titleLabel.font = CustomFont(13);
        [changeType1 setTitle:[UtilCheck language:@"setting.stock"] forState:UIControlStateNormal];
        [self.view addSubview:changeType1];
        [changeType1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        //        changeType1.backgroundColor = [UtilCheck hexStringToColor:@"E0E1E2" alpha:1.0];
        [changeType1 addTarget:self action:@selector(warehouseSearch:) forControlEvents:UIControlEventTouchUpInside];
        [changeType1 setFrame:CGRectMake(CGRectGetMaxX(changeType.frame), 28, 110, 28)];
        changeType1.backgroundColor = [UIColor whiteColor];
        changeType1.layer.cornerRadius = 5;
        [changeType1 setImage:BDFILEIMAGENAEM(@"Home/btn_dropGray") forState:UIControlStateNormal];
        changeType1.imageEdgeInsets = UIEdgeInsetsMake(10, 105, 10, 0);
        
    }

    {
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_HEIGHT, SCREEN_WIDTH - 64) style:(UITableViewStylePlain)];
      
        // headView
        self.headView = [[StockInquiryHeadView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 90)];
        //SRHeadView
        self.SRHeadView = [[ShipmentReportHeadView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 90)];
        self.tableView.backgroundColor = [UtilCheck hexStringToColor:@"F5F7f8" alpha:1];
        if ([_stockStr isEqualToString:@"1"]) {
            [self.tableView registerClass:[ShipmentReportCell class] forCellReuseIdentifier:@"ShipmentReportCell"];
        }else {
            [self.tableView registerClass:[StockInquiryCell class] forCellReuseIdentifier:@"stockInquiryCell"];
        }
        
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        
        [self.view addSubview:self.tableView];
    } 
}
#pragma mark -已收货报告,处理方法
//开始时间
- (void)startBut {
    tag = startBut.tag;
    [self selectionTime];
}
//结束时间
- (void)endBut {
    tag = endBut.tag;
    [self selectionTime];
}
//时间选择器
- (void)selectionTime {
    UIView *maskView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.height, self.view.width)];
    maskView.backgroundColor = [UIColor whiteColor];
    maskView.alpha = 0.2f;
    self.maskView = maskView;
    [self.view addSubview:self.maskView];
    
    //设置选择时间的TextField
    datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 216, self.view.frame.size.width, 216)];
    datePicker.backgroundColor = [UIColor whiteColor];
    datePicker.datePickerMode = UIDatePickerModeDateAndTime;
    datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:[UtilCheck language:@"datePicker.LocalID"]];
    
    toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 216 - 44, self.view.frame.size.width, 44.0f)];
    toolBar.backgroundColor = [UIColor grayColor];
    UIBarButtonItem* leftItem = [[UIBarButtonItem alloc] initWithTitle:[UtilCheck language:@"button.cancel"] style:UIBarButtonItemStyleDone target:self action:@selector(cancelAction)];
    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    UIBarButtonItem* rightItem = [[UIBarButtonItem alloc] initWithTitle:[UtilCheck language:@"button.done"] style:UIBarButtonItemStyleDone target:self action:@selector(confirmAction:)];
    [toolBar setItems:@[leftItem,flexSpace,rightItem]];
    [self.view addSubview:datePicker];
    [self.view addSubview:toolBar];
}
- (void)cancelAction {
    [self.maskView removeFromSuperview];
    [datePicker removeFromSuperview];
    [toolBar removeFromSuperview];
}
- (void)confirmAction:(UIBarButtonItem *)item {
    [self.maskView removeFromSuperview];
    [datePicker removeFromSuperview];
    [toolBar removeFromSuperview];
    
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd";
    //选择时间不能大于当前时间
    NSDate *selectedDate = datePicker.date;
    NSDate *nowDate = [NSDate date];
    if ([selectedDate earlierDate:nowDate] == selectedDate ) {
        [Toolobject showTextWithMessage:[UtilCheck language:@"datePicker.noBeforeNow"]]; //@"选择的时间不能早于当前时间"
        return;
    }
    if (tag == 101) {
        [startBut setTitle:[formatter stringFromDate:datePicker.date] forState:(UIControlStateNormal)];
        [_stockStrDict setValue:[formatter stringFromDate:datePicker.date] forKey:@"startTime"];
    }else {
        [endBut setTitle:[formatter stringFromDate:datePicker.date] forState:(UIControlStateNormal)];
        [_stockStrDict setValue:[formatter stringFromDate:datePicker.date] forKey:@"endTime"];
    }
    [self shipmentReport];
    
}
//输入框处理
- (void)textFieldDidEndEditing:(UITextField *)textField {
    if ([textField.text isEqualToString:@" "]) {
    }else {
        if (textField.tag == 201) {
            [_stockStrDict setValue:textField.text forKey:@"custName"];
        }else {
            [_stockStrDict setValue:textField.text forKey:@"searchKey"];
        }
        [self shipmentReport];
    }
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    
}

#pragma mark popView弹出

- (void)changeSearchType:(UIButton *)btn
{
    _coverView = [[UIView alloc]initWithFrame:self.view.frame];
    [_coverView setBackgroundColor:[THMangerTool getRZColor:250 green:250 blue:250 alpha:0.1]];
    [self.view addSubview:_coverView];
    if (!showPop) {
        
            StockPopVIewController *popView = [[StockPopVIewController alloc]init];
            popView.teger = @"1";
            popView.delegate = self;
            [popView.view setFrame:CGRectMake(50, CGRectGetMaxY(btn.frame), 180, 142)];
            [_coverView addSubview:popView.view];
            _popView = popView;
            showPop = YES;
    }
}

- (void)warehouseSearch:(UIButton *)btn {
    _coverView = [[UIView alloc]initWithFrame:self.view.frame];
    [_coverView setBackgroundColor:[THMangerTool getRZColor:250 green:250 blue:250 alpha:0.1]];
    [self.view addSubview:_coverView];
    if (!showPop) {
        StockPopVIewController *popView = [[StockPopVIewController alloc]init];
        popView.delegate = self;
        popView.popViewArr = stockArr;
        [popView.view setFrame:CGRectMake(150, CGRectGetMaxY(btn.frame), 180, 222)];
        [_coverView addSubview:popView.view];
        _popView = popView;
        showPop = YES;
    }
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    if (teger == 1) {
        [self.view endEditing:YES];
        teger = 0 ;
    }else {
        if (showPop == YES) {
            [_coverView removeFromSuperview];
            [_popView.view removeFromSuperview];
            [_popView removeFromParentViewController];
            showPop = NO;
        }
    }
}
//当键盘出现
- (void)keyboardWillShow:(NSNotification *)notification
{
    teger = 1;
}

- (void)didSelectPopCell:(NSInteger)row
{
    [_coverView removeFromSuperview];
    type = row+1;
    [_popView.view removeFromSuperview];
    _popView = nil;
    showPop = NO;
    self.postDict[@"searchKey"] = self.searchBar.text;
        switch (type) {
            case searchTypeFixpart:
                [_typeBtn setTitle:[UtilCheck language:@"order.search.fix"]  forState:UIControlStateNormal];
                [self InquiryInventory];
                break;
            case searchTypeAll:
                [_typeBtn setTitle:[UtilCheck language:@"order.search.all"] forState:UIControlStateNormal];
                [self InquiryInventory];
                
                break;
            case searchTypeFaultpart:
                [_typeBtn setTitle:[UtilCheck language:@"order.search.fault"] forState:UIControlStateNormal];
                [self InquiryInventory];
                
                break;
            default:
                break;
    }
}
- (void)didSelectPopCellTwo:(NSInteger)row setArr:(NSMutableArray *)arr{
    
        [_coverView removeFromSuperview];
        type = row+1;
        [_popView.view removeFromSuperview];
        _popView = nil;
        showPop = NO;
        self.postDict[@"storeName"] = stockArr[row];
        if (type == 1) {
            self.postDict[@"type"] = [NSString stringWithFormat:@"%d",type];
            self.postDict[@"storeName"] = arr[row];
            [_storeNameBtn setTitle:arr[row] forState:(UIControlStateNormal)];
        }
        else
        {
            self.postDict[@"type"] = [NSString stringWithFormat:@"%d",type-1];
            self.postDict[@"storeName"] = arr[row];
            [_storeNameBtn setTitle:arr[row] forState:(UIControlStateNormal)];
        }
        [self InquiryInventory];
   
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    self.postDict[@"searchKey"] = searchBar.text;
    
    if (type == 1) {
        self.postDict[@"type"] = [NSString stringWithFormat:@"%d",type];
        self.postDict[@"searchKey"] = searchBar.text;
    }
    else
    {
        self.postDict[@"type"] = [NSString stringWithFormat:@"%d",type-1];
        self.postDict[@"searchKey"] = searchBar.text;
    }
        [self InquiryInventory];
}

- (void)backView
{
    AppDelegate * app = [UIApplication sharedApplication].delegate;
    app.shouldChangeOrientation = NO;
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (UIImage*) GetImageWithColor:(UIColor*)color andHeight:(CGFloat)height
{
    CGRect r= CGRectMake(0.0f, 0.0f, 1.0f, height);
    UIGraphicsBeginImageContext(r.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, r);
    
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [_searchBar resignFirstResponder];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark- UITableViewDelegate / DataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    settleDeliveryUpCell *cell = (settleDeliveryUpCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
    if ([_stockStr isEqualToString:@"1"]) {
        ShipmentReportCell *cell = (ShipmentReportCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
        cell.textLabel.textColor = [UIColor blackColor];
        return cell.cellHeight;
    }else {
        StockInquiryCell *cell = (StockInquiryCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
        return cell.cellHeight;
    }

}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    StockInquiryCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    PlateModel *plate = dataArr[indexPath.row];
    StockInquryDetailVC *detailVC = [[StockInquryDetailVC alloc] init];
    detailVC.plate = plate;
    detailVC.title = [UtilCheck language:@"setting.stockInquiry"];
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:detailVC];
    AppDelegate * app = [UIApplication sharedApplication].delegate;
    app.shouldChangeOrientation = NO;
    [self presentViewController:nav animated:YES completion:nil];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return  dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([_stockStr isEqualToString:@"1"]) {
        ShipmentReportCell *cell = [[ShipmentReportCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"ShipmentReportCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.plateModel = dataArr[indexPath.row];
        [cell setPlateModel:dataArr[indexPath.row]];
        
        UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(10,cell.cellHeight - 0.5f, SCREEN_WIDTH - 20, 0.5f)];
        line.backgroundColor = [UtilCheck hexStringToColor:@"EcEcEc" alpha:1];
        [cell.contentView addSubview:line];
        return cell;
    }else {
        StockInquiryCell *cell = [[StockInquiryCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"stockInquiryCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.plateModel = dataArr[indexPath.row];
        [cell setPlateModel:dataArr[indexPath.row]];
        
        UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(10,cell.cellHeight - 0.5f, SCREEN_WIDTH - 20, 0.5f)];
        line.backgroundColor = [UtilCheck hexStringToColor:@"EcEcEc" alpha:1];
        [cell.contentView addSubview:line];
        return cell;
    }
}

#pragma mark- 网络请求
#pragma mark-

#pragma mark 库存查询
- (void)InquiryInventory {
//    self.postDict[@"storeName"] = @"北京SPM库";
    self.postDict[@"type"] = [NSString stringWithFormat:@"%d",type];
    NSLog(@"postdic ==== %@",self.postDict);
    
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];

    [[BDHUD sharedManager] showBDHUD:self.view title:nil];
    
        [AFHttpTool postJsonDatawithUrl:@"/inventory"  parameters:self.postDict success:^(id responseObject) {
            NSLog(@"responseobject ----- %@",responseObject);
            [[BDHUD sharedManager] dismissHUD];
            
            if(1 == [[[responseObject objectForKey:@"head"] objectForKey:@"status"] intValue] && [[responseObject objectForKey:@"data"] isKindOfClass:[NSArray class]]) {
                NSArray *arr = [responseObject objectForKey:@"data"];
                if (arr.count > 0) {
                    [dataArr removeAllObjects];
                    dataArr = [self modelWithDic:arr];
                    self.tableView.tableHeaderView = self.headView;
                    NSString *number = responseObject[@"count"];
                    NSString *countStr = [NSString stringWithFormat:[UtilCheck language:@"setting.plateCount"],number];
                    self.headView.titleLab.attributedText = [self attributedString:countStr];
                    [_searchBar resignFirstResponder];
                    [self.tableView reloadData];
                }
                else {
                    [dataArr removeAllObjects];
                    [self.tableView reloadData];
                    self.tableView.tableHeaderView = nil;
                    [[BDHUD sharedManager] showNOneDataViewHUD:self.tableView  top:30 title:[THMangerTool language:@"HUD.noData"]  onClick:nil];
                }
            }
            else {
                //返回数据出问题 弹出服务器返回信息
                if([[[responseObject objectForKey:@"head"] allKeys] containsObject:@"info"]&&![UtilCheck DataIsNuLL:[[responseObject objectForKey:@"head"] objectForKey:@"info"]]){
                    [[BDHUD sharedManager] showWords:[[responseObject objectForKey:@"head"] objectForKey:@"info"]];
                }
            }
            
        } fail:^(id responseObject, NSError *error) {
            NSLog(@"%@",error);
            [[BDHUD sharedManager] dismissHUD];
            if ([[error.userInfo objectForKey:@"NSLocalizedDescription"] isEqualToString:@"请求超时。"]) {
                [[BDHUD sharedManager] showWords:[UtilCheck language:@"order.time.out"]];
            }else {
                [[BDHUD sharedManager] showWords:[UtilCheck language:@"order.network.interruption"]];
            }
        }];
}
#pragma mark - 获取已发货报告
- (void)shipmentReport {

    NSLog(@"postdic ==== %@",self.stockStrDict);

    
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];

    [[BDHUD sharedManager] showBDHUD:self.view title:nil];
    
    [AFHttpTool postJsonDatawithUrl:@"/shipmentReport"  parameters:self.stockStrDict success:^(id responseObject) {
        NSLog(@"responseobject ----- %@",responseObject);
        [[BDHUD sharedManager] dismissHUD];
        
        if(1 == [[[responseObject objectForKey:@"head"] objectForKey:@"status"] intValue] && [[responseObject objectForKey:@"data"] isKindOfClass:[NSArray class]]) {
            NSArray *arr = [responseObject objectForKey:@"data"];
            if (arr.count > 0) {
                [dataArr removeAllObjects];
                dataArr = [self modelWithDic:arr];
                self.tableView.tableHeaderView = self.SRHeadView;
                NSString *number = responseObject[@"count"];
                NSString *countStr = [NSString stringWithFormat:[UtilCheck language:@"setting.total"],number];
                self.SRHeadView.titleLab.attributedText = [self attributedString:countStr];
                [_searchBar resignFirstResponder];
                [self.tableView reloadData];
            }
            else {
                [dataArr removeAllObjects];
                [self.tableView reloadData];
                self.tableView.tableHeaderView = nil;
                [[BDHUD sharedManager] showNOneDataViewHUD:self.tableView  top:30 title:[THMangerTool language:@"HUD.noData"]  onClick:nil];
            }
        }
        else {
            //返回数据出问题 弹出服务器返回信息
            if([[[responseObject objectForKey:@"head"] allKeys] containsObject:@"info"]&&![UtilCheck DataIsNuLL:[[responseObject objectForKey:@"head"] objectForKey:@"info"]]){
                [[BDHUD sharedManager] showWords:[[responseObject objectForKey:@"head"] objectForKey:@"info"]];
            }
        }
        
    } fail:^(id responseObject, NSError *error) {
        NSLog(@"%@",error);
        [[BDHUD sharedManager] dismissHUD];
        if ([[error.userInfo objectForKey:@"NSLocalizedDescription"] isEqualToString:@"请求超时。"]) {
            [[BDHUD sharedManager] showWords:[UtilCheck language:@"order.time.out"]];
        }else {
            [[BDHUD sharedManager] showWords:[UtilCheck language:@"order.network.interruption"]];
        }
    }];
}

#pragma mark - 获取库存按钮信息
- (void)stockInquiry {
    NSMutableDictionary *stockDictionary = [NSMutableDictionary dictionaryWithCapacity:0];
    [stockDictionary setValue:[THMangerTool getKey] forKey:@"key"];
    [AFHttpTool postJsonDatawithUrl:@"inventoryForStore" parameters:stockDictionary success:^(id responseObject) {
        if (1 == [[[responseObject objectForKey:@"head"] objectForKey:@"status"] intValue]) {
            stockArr = [responseObject objectForKey:@"data"];
            NSLog(@"我们%lu",stockArr.count);
            
        }
    } fail:^(id responseObject, NSError *error) {
        if ([[error.userInfo objectForKey:@"NSLocalizedDescription"] isEqualToString:@"请求超时。"]) {
            [[BDHUD sharedManager] showWords:[UtilCheck language:@"order.time.out"]];
        }else {
            [[BDHUD sharedManager] showWords:[UtilCheck language:@"order.network.interruption"]];
        }
    }];
}
- (NSMutableArray *)modelWithDic:(NSArray *)arr
{
    NSMutableArray *muArr = [NSMutableArray array];
    for (int i = 0 ; i < arr.count; i ++) {
        NSMutableDictionary *dic = arr[i];
        dic[@"number"] = [NSString stringWithFormat:@"%@",dic[@"number"]];
        PlateModel *plate = [[PlateModel alloc]init];
//        plate.warehouse = @"xx库房";
        [plate setValuesForKeysWithDictionary:dic];
        [muArr addObject:plate];
    }
    return muArr;
}

- (void)touchSearchBtn:(UIButton *)sender {
   // self.searchHoldView.hidden= YES;
}

#pragma mark- 其他事件
#pragma mark-

/**
 *  更改字符串的前两个空格之间字符串的颜色
 *
 *  @param txt 要改变颜色的字符床
 1231
 *
 *  @return 改变后的字符串
 */
- (NSMutableAttributedString *)attributedString:(NSString *)txt {
    
    // 查找出前两个空格的位置
    NSInteger start = 0;
    NSInteger end = 0;
    NSRange countRange = NSMakeRange(0, 0);
    
    for (int i = 0 ; i < txt.length; i ++) {
        if ([txt characterAtIndex:i] == 32 && start == 0) {
            start = i;

        }
        else if([txt characterAtIndex:i] == 32){
            end = i;
            countRange = NSMakeRange(start + 1, end - start);
            break;
        }
    }
    // 改变范围内字符颜色
    NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:txt];
    [attributeStr addAttribute:NSForegroundColorAttributeName value:[UtilCheck hexStringToColor:@"40BFF5" alpha:1] range:countRange];

    return attributeStr;
}


//- (BOOL)shouldAutorotate
//{
//    return NO;
//}
//
//- (UIInterfaceOrientationMask)supportedInterfaceOrientations
//{
//    return UIInterfaceOrientationMaskLandscape;
//}
//
//- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
//{
//    return UIInterfaceOrientationLandscapeRight;
//}



@end
